package longrise.phone.com.bjjt_jyb.widget;


import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.compensate.ProcessInfo;


public class AlertDialogRecordSec {

	private Context context;
	private AlertDialogRecord2 dialog;
	private Display display;
	private ListView lv_sec;
	private MySelectAdapter adapter;
	private String packageName;
	private Button bt_sec;

	public AlertDialogRecordSec(Context context) {
		this.context = context;
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();
	}

	public AlertDialogRecordSec builder(){
		// 重写Dialog
		View view = LayoutInflater.from(context).inflate(R.layout.pub_popwindow_sec, null);
		lv_sec = (ListView) view.findViewById(R.id.lv_sec);
		bt_sec = (Button) view.findViewById(R.id.bt_sec);
		initProcessList();
		adapter = new MySelectAdapter(context);
		lv_sec.setAdapter(adapter);
		// Dialog
		dialog = new AlertDialogRecord2(context, R.style.AlertDialogStyle);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		bt_sec.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				clear();
				if (currentSelectType == -1) {
					UiUtil.showToast(context, "请选择违法条款");
						return;
				}
				onitemsel.onItemsec(packageName,mySelect);
				dialog.dismiss();
			}
		});
		// dialog
		//lv_sec.setLayoutParams(new LinearLayout.LayoutParams((int) (display.getWidth() * 0.45), LayoutParams.WRAP_CONTENT));
		return this;
	}


	public void dismiss() {
		if(dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	public void show() {
		dialog.show();
	}

	private List<ProcessInfo> userProcessInfos;
	private String[] accidentAll= new String[]{"同车道行驶的车辆追撞前车尾部的","借道通行或变更车道时未让正常行驶的车辆先行的","通过没有交通信号的交叉路口时，未让优先通行的一方先行的",
			"相对方向来车左转弯车辆未让直行车辆，或右转弯车辆未让左转弯车辆先行的","不按照交通信号指示通行的","交叉路口先放行车辆未驶离路口时，后放行车辆未让行的",
			"在道路上会车不按规定通行或让行的","逆向行驶的","不按规定强行超车的","行经交叉路口、窄桥、弯道、陡坡、隧道、人行横道路段时超车的","在只有一条机动车道的道路上，从前车右侧超越的",
			"遇前方车辆停车排队或者缓慢行驶时，从前方车辆两侧穿插或者超越行驶的","在车道减少的路口、路段，遇前方车辆停车排队或者缓慢行驶的，不依次交替驶入路口、路段的","在禁止掉头或者在允许掉头的地方未让正常行驶车辆先行的",
			"倒车、溜车、车辆失控与其他车辆发生碰撞的","在专用车道内行驶或驶入禁行线的","开关车门造成交通事故的","车辆进出停车场、停车位或者由路边、非机动车道进出道路与正常行驶车辆、停放车辆发生交通事故的，及在机动车道上违法停车的",
			"车辆装载货物超长、超宽、超高部分或者货物在遗洒、飘散过程中导致交通事故的","不按规定牵引车辆造成交通事故的","单方发生交通事故的","未确保安全的其他情形"};
	private void initProcessList() {
		//创建集合为CopyOnWriteArrayList,可以防止在集合遍历的过程中抛出ConcurrentModificationException(并发修改异常)
		//底层两个数组,一个源数据,一个拷贝数组,源数组用于遍历,拷贝数组用于修改
		//userProcessInfos = new CopyOnWriteArrayList<ProcessInfo>();
		userProcessInfos = new ArrayList<>();
		for(int i=0;i<22;i++){
			userProcessInfos.add(new ProcessInfo(accidentAll[i],false));
		}
	}

	private onitemsecl onitemsel;

	public onitemsecl getOnitemsel() {
		return onitemsel;
	}

	public void setOnitemsel(onitemsecl onitemsel) {
		this.onitemsel = onitemsel;
	}

	public interface onitemsecl{
	void  onItemsec(String text,String i);
	}

	private class MySelectAdapter extends BaseAdapter {
		private final Context context;

		public MySelectAdapter(Context context) {
			this.context = context;
		}
		@Override
		public int getCount() {
			return userProcessInfos.size();
		}

		@Override
		public Object getItem(int position) {

			return null;
		}

		@Override
		public long getItemId(int position) {

			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AlertDialogRecordSec.ViewHolder holder = null;
			if (convertView != null) {
				holder = (AlertDialogRecordSec.ViewHolder) convertView.getTag();
			} else {
				convertView = LayoutInflater.from(context).inflate(R.layout.lv_process_list_item2, null);
				holder = new AlertDialogRecordSec.ViewHolder();
				holder.tv_process_name = (TextView) convertView.findViewById(R.id.tv_process_name2);
				holder.cb_checked = (CheckBox) convertView.findViewById(R.id.cb_checked2);
				convertView.setTag(holder);
			}
			final ProcessInfo info = userProcessInfos.get(position);
			holder.cb_checked.setChecked(info.isChecked());
			holder.cb_checked.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					CheckBox cb = (CheckBox) v;
					if(info.isChecked()){
						cb.setChecked(false);
						info.setChecked(cb.isChecked());
					}
					//遍历选中的条目
					countselecttotleitem();
					if(selecteditem>2){
						UiUtil.showToast(context,"违法条款最多选择三个");
						cb.setChecked(false);
						info.setChecked(cb.isChecked());
						return;
					}
					// 将勾选状态存入info中
					info.setChecked(cb.isChecked());
				}
			});
			holder.tv_process_name.setText(info.getPackageName());
			return convertView;
		}

	}
	private class ViewHolder {
		private TextView tv_process_name;
		private CheckBox cb_checked;
	}
	private List<Integer> mySelectType=new ArrayList<>();
	private String mySelect;
	/**
	 * 当处于选择状态应该怎么做
	 */
	public void clear(){
		for(int i=0;i<userProcessInfos.size();i++){
			if(userProcessInfos.get(i).isChecked()){
				if(i<9){
					currentSelectType=40+i+1;//当这个选中，那么事故的类型就是他的index
				}else {
					currentSelectType=400+i+1;//当这个选中，那么事故的类型就是他的index
				}

				if(null!=mySelect){
					mySelect=mySelect+","+currentSelectType;
					packageName=packageName+","+userProcessInfos.get(i).getPackageName();
				}else {
					mySelect=String.valueOf(currentSelectType);
					packageName=userProcessInfos.get(i).getPackageName();
				}
			}
		}
	}
	// 当前选择
	private int currentSelectType = -1;
	private int selecteditem = 0;
	private void countselecttotleitem() {
		selecteditem=0;
		for(int i=0;i<userProcessInfos.size();i++){
			if(userProcessInfos.get(i).isChecked()){
				selecteditem++;
			}
		}
	}
}
